<?php
/***********************************************************************
 * @package     IvanEngine
 * @subpackage  Framwork
 * @author      Ivan <ivanzx@msn.com>
 * @created     2010-05-30
 ***********************************************************************/
if(defined("IVANENGINE_FRAMEWORK_I18NMANAGER"))
    die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_FRAMEWORK_I18NMANAGER", true);

require_once (dirname(__FILE__).'/Common.php');

class CI18NManager extends CSingleton
{
    const LT_AF         = 'af';     // Afrikaans
    const LT_AR         = 'ar';     // Arabic
    const LT_AZ         = 'az';     // Azerbaijani
    const LT_BG         = 'bg';     // Bulgarian
    const LT_BS         = 'bs';     // Bosnian
    const LT_CA         = 'ca';     // Catalan
    const LT_CS         = 'cs';     // Czech
    const LT_DA         = 'da';     // Danish
    const LT_DE         = 'de';     // German
    const LT_EL         = 'el';     // Greek
    const LT_EN         = 'en';     // English
    const LT_EO         = 'eo';     // Esperanto
    const LT_ES         = 'es';     // Spanish
    const LT_ET         = 'et';     // Estonian
    const LT_EU         = 'eu';     // Euskarako (Basque)
    const LT_FA         = 'fa';     // Persian (Farsi)
    const LT_FI         = 'fi';     // Finnish
    const LT_FO         = 'fo';     // Faroese
    const LT_FR_CH      = 'fr-CH';  // Swiss-French
    const LT_FR         = 'fr';     // French
    const LT_HE         = 'he';     // Hebrew
    const LT_HR         = 'hr';     // Croatian 
    const LT_HU         = 'hu';     // Hungarian
    const LT_HY         = 'hy';     // Armenian
    const LT_ID         = 'id';     // Indonesian
    const LT_IS         = 'is';     // Icelandic
    const LT_IT         = 'it';     // Italian
    const LT_JA         = 'ja';     // Japanese
    const LT_KO         = 'ko';     // Korean
    const LT_LT         = 'lt';     // Lithuanian
    const LT_LV         = 'lv';     // Latvian
    const LT_MS         = 'ms';     // Malaysian
    const LT_NL         = 'nl';     // Dutch
    const LT_NO         = 'no';     // Norwegian
    const LT_PL         = 'pl';     // Norwegian
    const LT_PT_BR      = 'pt-BR';  // Brazilian
    const LT_RO         = 'ro';     // Romanian
    const LT_RU         = 'ru';     // Russian
    const LT_SK         = 'sk';     // Slovak
    const LT_SL         = 'sl';     // Slovenian
    const LT_SQ         = 'sq';     // Albanian
    const LT_SR_SR      = 'sr-SR';  // Serbian
    const LT_SR         = 'sr';     // Serbian
    const LT_SV         = 'sv';     // Swedish
    const LT_TA         = 'ta';     // Tamil
    const LT_TH         = 'th';     // Thai
    const LT_TR         = 'tr';     // Turkish
    const LT_UK         = 'uk';     // Ukrainian
    const LT_VI         = 'vi';     // Vietnamese
    const LT_ZH_CN      = 'zh-CN';  // Chinese
    const LT_ZH_HK      = 'zh-HK';  // Hongkong
    const LT_ZH_TW      = 'zh-TW';  // TaiWan
    
    const TAG           = 'I18N';
    static protected $m_this    = null;
    protected
        $m_standardPackage  = null,
        $m_commonPackage    = null,
        $m_customPackage    = null,
        $m_language         = self::LT_EN;
        
    public function __construct()
    {
        parent::__construct();
        $this->m_standardPackage    = new CMap();
        $this->m_commonPackage      = new CMap();
        $this->m_customPackage      = new CMap();
    }
    
    public function __destruct()
    {
        unset($this->m_standardPackage);
        unset($this->m_commonPackage);
        unset($this->m_customPackage);
        parent::__destruct();
    }
    
    static public function Get()
    {
        if( self::$m_this == null )
        {
            parent::StartSingleton();
            self::$m_this = new CI18NManager();
            parent::EndSingleton();
        }
        return self::$m_this;
    }
    
    public function Initialize($language)
    {
        $this->m_language   = $language;

        // load standard language
        $standardXml        = M_ENGINE_PATH.'I18N'.
            M_DS.$language.'.xml';
        $standardCache      = CCacheManager::Get()->
            GetCacheFilePath('I18N'.M_DS.$language.'.dat');
        CCacheManager::Get()->AddCacheFile($standardXml, 
            new CCacheCallback('_LoadStandardLanguage', $this),
            $standardCache);
        
        // load common language
        $commonXml    = CConfig::Option(CConfig::OT_PluginPath).M_DS.
            'I18N'.M_DS.$language.'.xml';
        $commonCache  = CCacheManager::Get()->GetCacheFilePath(
            'Plugins'.M_DS.'I18N'.M_DS.$language.'.dat');
        CCacheManager::Get()->AddCacheFile(
            $commonXml, new CCacheCallback('_LoadCommonLanguage', $this));
    }
    
    public function AddPackage($packagePath, $ignore = false, $cachePath = null)
    {
        $packageFile    = $packagePath.M_DS.$this->m_language.'.xml';
        $packageFile    = CFileHelper::CorrectPath($packageFile);
        if( $cachePath ){
            $cachePath  = $cachePath.M_DS.$this->m_language.'.dat';
            if( !CFileHelper::IsAbsolutePath($cachePath) ){
                $cacheFile      = CCacheManager::Get()->GetCacheFilePath($cacheFile);
            }
        }
        
        CCacheManager::Get()->AddCacheFile(
            $packageFile, 
            new CCacheCallback('_LoadCustomLanguage', $this), 
            $cachePath, 
            $ignore);
    }
    
    public function __($key)
    {
        return $this->Language($key);
    }
    
    public function Language($key)
    {
        $result = $this->m_standardPackage->Get($key);
        if( $result ){
            return $result;
        }
        $result = $this->m_commonPackage->Get($key);
        if( $result ){
            return $result;
        }
        $result = $this->m_customPackage->Get($key);
        if( $result ){
            return $result;
        }
        return null;
    }
    
    public function LanguageFromStandard($key)
    {
        return $this->m_standardPackage->Get($key);
    }
    
    public function LanguageFromCommon($key)
    {
        return $this->m_commonPackage->Get($key);
    }
    
    public function LanguageFromCustom($key)
    {
        return $this->m_customPackage->Get($key);
    }
    
    public function GetStandard()
    {
        return $this->m_standardPackage;
    }
    
    public function GetCommon()
    {
        return $this->m_commonPackage;
    }
    
    public function GetCustom()
    {
        return $this->m_customPackage;
    }
    
    public function GetLanguage()
    {
        return $this->m_language;
    }
    
    static public function FormatDataTime($datatime)
    {
        $language = CI18NManager::Get()->GetLanguage();
        return $datatime;
    }
    
    static public function FormatNumber($number)
    {
        $language = CI18NManager::Get()->GetLanguage();
        return $number;
    }
    
    public function _LoadStandardLanguage($isGenerate, $origin, $cache)
    {
        if( $isGenerate )
        {
            $xml    = new CXml(CXml::NST_ATTR, 'name');
            $xml->Load($origin);
            $this->m_standardPackage    
                    = $xml->GetRoot()->GetChilds();
            $content= serialize($this->m_standardPackage);
            CFileHelper::Write($cache, $content);
            unset($xml);
        }else{
            $content= CFileHelper::Read($cache);
            $this->m_standardPackage
                    = unserialize($content);
        }
    }
    
    public function _LoadCommonLanguage($isGenerate, $origin, $cache)
    {
        if( $isGenerate )
        {
            $xml    = new CXml(CXml::NST_ATTR, 'name');
            $xml->Load($origin);
            $this->m_commonPackage    
                    = $xml->GetRoot()->GetChilds();
            $content= serialize($this->m_commonPackage);
            CFileHelper::Write($cache, $content);
            unset($xml);
        }else{
            $content= CFileHelper::Read($cache);
            $this->m_commonPackage
                    = unserialize($content);
        }
    }
    
    public function _LoadCustomLanguage($isGenerate, $origin, $cache)
    {
        if( $isGenerate )
        {
            $xml    = new CXml(CXml::NST_ATTR, 'name');
            $xml->Load($origin);
            $tmpMap = $xml->GetRoot()->GetChilds();
            $this->m_customPackage->Append($tmpMap);   
            $content= serialize($tmpMap);
            CFileHelper::Write($cache, $content);
            unset($xml);
        }else{
            $content= CFileHelper::Read($cache);
            if( strlen($content) > 0 ){
                $this->m_customPackage->Append(unserialize($content));
            }
        }
    }
}
?>
